//
//  NetEaseTitleLayout.m
//  15-NetEaseHomeWork
//
//  Created by DM.C  on 16/8/3.
//  Copyright © 2016年 DM.C . All rights reserved.
//

#import "NetEaseTitleLayout.h"

@interface NetEaseTitleLayout ()
@property (nonatomic, strong) NSMutableArray *attributesArray;
@property (nonatomic, assign) CGFloat contentWidth;
@end

@implementation NetEaseTitleLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _titleFont = [UIFont systemFontOfSize:17];
        _contentWidth = 0;
    }
    return self;
}

- (void)prepareLayout {
    CGFloat itemWidth = 0;
    CGFloat x = 0;
//    if (_attributesArray.count != 0) {
//        [_attributesArray removeAllObjects];
//    }
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        x += itemWidth;
        itemWidth = [self.delegate collectionView:self.collectionView layout:self string:_title stringFont:_titleFont widthForItemAtIndexPath:indexPath] + 20;
        itemWidth = ceilf(itemWidth);
        CGFloat y = 0;
        CGFloat height = 44;
        self.contentWidth = x + itemWidth;
        layoutAttributes.frame = CGRectMake(x, y, itemWidth, height);
        [_attributesArray addObject:layoutAttributes];
    }
}


- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attributesArray;
}

- (CGSize)collectionViewContentSize {
    return CGSizeMake(_contentWidth, 0);
}



@end
